#include<iostream>
#include<fstream>
#include<string>
#include<stack>
#include<algorithm>
#include"include/NumAndCal.hpp"
using namespace std;

//运算优先级
char *priorities[] =
{
	//       #+-*/()
	/* # */ "E<<<<<X",
	/* + */ ">>><<<>",
	/* - */ ">>><<<>",
	/* * */ ">>>>><>",
	/* / */ ">>>>><>",
	/* ( */ "X<<<<<=",
	/* ) */ "XXXXXXX",
};

//给运算符编号
int getNum(char c)
{
    switch(c)
    {
        case '#':return 0;
        case '+':return 1;
        case '-':return 2;
        case '*':return 3;
        case '/':return 4;
        case '(':return 5;
        case ')':return 6;
    }
}

void print_opstack(stack<char> op){
    stack<char> myop {op};
    while(!myop.empty()){
        cout<<myop.top()<<" ";
        myop.pop();
    }
    // cout<<endl;
}

//中缀转后缀表达式
string convert(string s)
{
    string post="";//存放后缀表达式
    stack<char>op;//运算符栈
    op.push('#');

    for(int i=0;i<s.size();i++)
    {
        cout<<"-----------------"<<endl;
        cout<<"the letter is "<<s[i]<<":"<<endl;
        cout<<"the op stack is:    ";
        print_opstack(op);
        cout<<endl;
        cout<<"the numbers is: "<<post<<endl;
        
        //cout<<op.top()<<" "<<post<<endl;
        //数字
        if(isdigit(s[i]))
        {
            post+=s[i];
        }

        //运算符
        else
        {
            switch(s[i])
            {
            case '(':
                op.push(s[i]);
                break;
            case ')':

                //将栈顶运算符弹出并输出，直到遇到左括号
                while(!op.empty()&&op.top()!='(')
                {
                    post+=op.top();
                    op.pop();
                }
                op.pop();//弹出左括号，不输出
                break;
            default:
                //优先级大于栈顶优先级
                if(op.top()=='('||(op.top()!='('&&priorities[getNum(op.top())][getNum(s[i])]=='<'))
                {
                    op.push(s[i]);
                }
                //优先级小于栈顶优先级
                else
                {
                    while(priorities[getNum(op.top())][getNum(s[i])]=='>')
                    {
                        post+=op.top();
                        op.pop();
                    }
                    op.push(s[i]);
                }
                break;
            }
        }
    }
cout<<"-----------------"<<endl;
        // cout<<"the letter is "<<s[i]<<":"<<endl;
        cout<<"the op stack is:    ";
        print_opstack(op);
        cout<<endl;
        cout<<"the numbers is: "<<post<<endl;
    //栈中残留运算符加入后缀表达式
    while(!op.empty())
    {
        post+=op.top();
        op.pop();
    }
    post.pop_back();//去掉'#'
    return post;
}

int calculation(int a,char op,int b)
{
    switch(op)
    {
    case '+':
        return a+b;
    case '-':
        return a-b;
    case '*':
        return a*b;
    case '/':
        return a/b;
    }
}
//计算后缀表达式的值
int calcu(string s)
{
    stack<int>num;
    for(int i=0;i<s.size();i++)
    {
        //数字压栈
        if(isdigit(s[i]))
        {
            num.push(s[i]-48);//char转int,ASCII码减48
        }
        //运算符
        else
        {
            int a,b;
            //从栈顶取两个运算数,做运算
            if(!s.empty())
            {
                a=num.top();
                num.pop();
            }
            if(!s.empty())
            {
                b=num.top();
                num.pop();
            }
            int res=calculation(b,s[i],a);//计算
            num.push(res);//结果压栈
        }
    }
    return num.top();
}

int main()
{
    ifstream input("input.txt");//读入含有算数表达式的文件
    if(!input)
    {
        cout<<"Failed"<<endl;
        return -1;
    }


    char c;
    string mexp;//存放读出的中缀表达式

    //读取文件中表达式
    while(input.get(c))
    {
        mexp.push_back(c);
    }
    cout<<mexp<<endl;
    string pexp=convert(mexp);//pexp存放后缀表达式
    cout<<pexp<<endl;
    cout<<calcu(pexp);//计算后缀表达式，输出结果

    input.close();
    return 0;
}

